package com.bristua.barcode.customer.controller;

import com.bristua.barcode.common.bean.PageBean;
import com.bristua.barcode.common.bean.Result;
import com.bristua.barcode.common.util.HandleResultUtil;
import com.bristua.barcode.customer.biz.GetCustomerIdBiz;
import com.bristua.barcode.customer.dao.TCustomerTakeAmtMapper;
import com.bristua.barcode.customer.model.TCustomerTakeAmt;
import com.bristua.barcode.customer.model.condition.QueryTakeCashRecordParams;
import com.bristua.barcode.customer.model.condition.TCustomerTakeAmtExample;
import com.bristua.barcode.supplier.model.TSupplier;
import com.bristua.barcode.supplier.model.condition.TSupplierExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2019/8/27.
 */
@RestController
public class QueryTakeCashRecordController {

    @Resource
    private TCustomerTakeAmtMapper tCustomerTakeAmtMapper;

    @Autowired
    private GetCustomerIdBiz getCustomerIdBiz;

    @PostMapping(value = "/query/take/cash/record")
    public Result queryTakeCashRecord(@RequestBody QueryTakeCashRecordParams queryTakeCashRecordParams){

        TCustomerTakeAmtExample tCustomerTakeAmtExample = new TCustomerTakeAmtExample();

        TCustomerTakeAmtExample.Criteria criteria = tCustomerTakeAmtExample.createCriteria();

        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getRealName())){
            criteria.andRealNameLike("%"+queryTakeCashRecordParams.getRealName()+"%");
        }

        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getStartTime())){
            criteria.andCreateTimeGreaterThanOrEqualTo(queryTakeCashRecordParams.getStartTime());
        }

        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getEndTime())){
            criteria.andCreateTimeLessThanOrEqualTo(queryTakeCashRecordParams.getEndTime());
        }

        if(queryTakeCashRecordParams.getStatus()!=null){
            criteria.andStatusEqualTo(queryTakeCashRecordParams.getStatus());
        }

        tCustomerTakeAmtExample.setOrderByClause("create_time desc");

        Page<TCustomerTakeAmt> objects = PageHelper.startPage(queryTakeCashRecordParams.getPageNo(),queryTakeCashRecordParams.getPageSize());

        tCustomerTakeAmtMapper.selectByExample(tCustomerTakeAmtExample);

        return HandleResultUtil.querySuccessResult(new PageBean<>(objects));

    }



    @PostMapping(value = "/customer/query/take/cash/record")
    public Result queryCustomerTakeCashRecord(@RequestBody QueryTakeCashRecordParams queryTakeCashRecordParams){

        TCustomerTakeAmtExample tCustomerTakeAmtExample = new TCustomerTakeAmtExample();
        TCustomerTakeAmtExample.Criteria criteria = tCustomerTakeAmtExample.createCriteria();
        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getRealName())){
            criteria.andRealNameLike("%"+queryTakeCashRecordParams.getRealName()+"%");
        }
        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getStartTime())){
            criteria.andCreateTimeGreaterThanOrEqualTo(queryTakeCashRecordParams.getStartTime());
        }
        if(!StringUtils.isEmpty(queryTakeCashRecordParams.getEndTime())){
            criteria.andCreateTimeLessThanOrEqualTo(queryTakeCashRecordParams.getEndTime());
        }

        if(queryTakeCashRecordParams.getStatus()!=null){
            criteria.andStatusEqualTo(queryTakeCashRecordParams.getStatus());
        }

        criteria.andCustomerIdEqualTo(getCustomerIdBiz.getCustomerId());
        tCustomerTakeAmtExample.setOrderByClause("create_time desc");

        Page<TSupplier> objects = PageHelper.startPage(queryTakeCashRecordParams.getPageNo(),queryTakeCashRecordParams.getPageSize());

        tCustomerTakeAmtMapper.selectByExample(tCustomerTakeAmtExample);

        return HandleResultUtil.querySuccessResult(new PageBean<>(objects));

    }

}
